package com.the_ring.chapter07.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.the_ring.chapter07.BillAddActivity;
import com.the_ring.chapter07.R;
import com.the_ring.chapter07.bean.BillInfo;
import com.the_ring.chapter07.database.DatabaseHelper;

import java.util.List;

public class BillItemAdapter extends BaseAdapter implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private Context mContext;
    private List<BillInfo> infoList;


    public BillItemAdapter(Context context, List<BillInfo> list) {
        super();
        mContext = context;
        infoList = list;
    }

    @Override
    public int getCount() {
        return infoList.size();
    }

    @Override
    public BillInfo getItem(int position) {
        return infoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // 获取指定位置的列表视图
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();  // 创建一个视图持有者
            // 根据布局生成转换视图对象
            convertView = LayoutInflater.from(mContext).inflate(R.layout.bill_item, null);
            holder.dateTv = convertView.findViewById(R.id.tv_date);
            holder.detailTv = convertView.findViewById(R.id.tv_detail);
            holder.moneyTv = convertView.findViewById(R.id.tv_money);
            convertView.setTag(holder);     // 将视图持有者保存到转换视图中
        } else {
            // 从转换视图中获取之前保存的视图持有者
            holder = (ViewHolder) convertView.getTag();
        }

        BillInfo info = infoList.get(position);
        holder.dateTv.setText(info.date);
        holder.detailTv.setText(info.detail);
        if (info.type == 1) {
            holder.moneyTv.setText("收入" + String.valueOf(info.money) + "元");
        } else {
            holder.moneyTv.setText("支出" + String.valueOf(info.money) + "元");
        }
        return convertView;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // 编辑数据
        BillInfo info = infoList.get(position);
        Intent intent = new Intent(mContext, BillAddActivity.class);
        intent.putExtra("id", info.id);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        mContext.startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        // 删除数据
        BillInfo info = infoList.get(position);
        String desc = String.format("是否删除以下账单？\n%s %s%d %s", info.date, info.type == 1 ? "收入" : "支出", info.money, info.detail);
        new AlertDialog.Builder(mContext)
                .setMessage(desc)
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        deleteBill(position);
                    }
                })
                .setNegativeButton("否", null)
                .create().show();
        return true;
    }

    private void deleteBill(int position) {
        DatabaseHelper.getInstance(mContext).delete(infoList.get(position).id);
        infoList.remove(position);
        notifyDataSetChanged();
    }

    public final class ViewHolder {
        public TextView dateTv;
        public TextView detailTv;
        public TextView moneyTv;
    }
}
